
#include "memchunkdialog.h"
#include "rangeselectdialog.h"
#include "ui_rangeselectdialog.h"

RangeSelectDialog::RangeSelectDialog( u32 addr, u32 len, u32 &viewMode, QWidget *parent ) :
    QDialog(parent),
    ui( new Ui::RangeSelectDialog )
{
    ui->setupUi( this );

    ui->lineEdit_addr->setText( QString( "%1" ).arg( addr, 8, 16, QChar( '0' ) ) );
    ui->lineEdit_len->setText( QString( "%1" ).arg( len, 8, 16, QChar( '0' ) ) );

    if( viewMode & MemChunkDialog::V_AbsoluteOffset )
    {
        ui->radioButton_addrAbsolute->setChecked( true );
    }
    else if( viewMode & MemChunkDialog::V_RelativeOfset )
    {
        ui->radioButton_addrRelative->setChecked( true );
    }
    else
    {
        ui->radioButton_addrNone->setChecked( true );
    }

    ui->checkBox_ascii->setChecked( viewMode & MemChunkDialog::V_Ascii );
}

RangeSelectDialog::~RangeSelectDialog()
{
    delete ui;
}

bool RangeSelectDialog::GetRange( u32 &addr, u32 &len, u32 &viewMode, QWidget *parent )
{
    RangeSelectDialog r( addr, len, viewMode, parent );
    if( r.exec() )
    {
        addr = r.ui->lineEdit_addr->text().toUInt( NULL, 16 );
        len = r.ui->lineEdit_len->text().toUInt( NULL, 16 );

        u32 vm = 0;
        if( r.ui->radioButton_addrAbsolute->isChecked() )
        {
            vm = MemChunkDialog::V_AbsoluteOffset;
        }
        else if( r.ui->radioButton_addrRelative->isChecked() )
        {
            vm = MemChunkDialog::V_RelativeOfset;
        }

        if( r.ui->checkBox_ascii->isChecked() )
        {
            vm |= MemChunkDialog::V_Ascii;
        }

        viewMode = vm;
        return true;
    }
    return false;
}

